﻿using Basic.FormCollect;
using Basic.FormModel.Column;
using Basic.FormModel.Group;
using Basic.FormRemoteModel.Table.Model;
using Basic.FormService.Interface;
using WeDonekRpc.Helper;

namespace Basic.FormService.Extend
{
    internal class FormQueryBodyService : IFormQueryBodyService
    {
        private readonly ITableGroupCollect _TableGroup;
        private readonly ITableColumnCollect _TableColumn;
        private readonly IControlCollect _Control;
        public FormQueryBodyService ( ITableGroupCollect tableGroup,
            IControlCollect control,
            ITableColumnCollect tableColumn )
        {
            this._Control = control;
            this._TableGroup = tableGroup;
            this._TableColumn = tableColumn;
        }
        private TableGroupBase[] _Groups;
        private ColumnDto[] _Columns;
        private Dictionary<long, string> _colName;
        public FormQueryColumn[] Gets ( long tableId )
        {
            this._Groups = this._TableGroup.GetsByTableId<TableGroupBase>(tableId);
            this._Columns = this._TableColumn.GetsByTableId<ColumnDto>(tableId);
            if ( this._Groups.IsNull() && this._Columns.IsNull() )
            {
                return null;
            }
            this._colName = this._Control.GetNames(this._Columns.Distinct(a => a.ControlId));
            if ( this._Groups.IsNull() )
            {
                return this._Columns.OrderBy(a => a.Sort).Select(a => new FormQueryColumn
                {
                    Id = a.Id,
                    Title = a.ColTitle,
                    IsGroup = false,
                    ControlName = this._colName.GetValueOrDefault(a.ControlId),
                    DefaultVal = a.DefaultVal,
                    Description = a.Description,
                    IsNotNull = a.IsNotNull,
                    Name = a.ColName,
                    Width = a.Width,
                    Sort = a.Sort
                }).ToArray();
            }
            return this._GetChildren(0);
        }
        private FormQueryColumn[] _GetChildren ( long prtId )
        {
            TableGroupBase[] groups = this._Groups.FindAll(c => c.ParentId == prtId);
            ColumnDto[] cols = this._Columns.FindAll(c => c.GroupId == prtId);
            if ( groups.IsNull() && cols.IsNull() )
            {
                return null;
            }
            List<FormQueryColumn> list = new List<FormQueryColumn>();
            groups.ForEach(a =>
            {
                list.Add(new FormQueryColumn
                {
                    Id = a.Id,
                    Title = a.GroupName,
                    IsGroup = true,
                    IsHidden = a.IsHidden,
                    Sort = a.Sort,
                    Children = this._GetChildren(a.Id)
                });
            });
            cols.ForEach(a =>
            {
                list.Add(new FormQueryColumn
                {
                    Id = a.Id,
                    Title = a.ColTitle,
                    IsGroup = false,
                    ControlName = this._colName.GetValueOrDefault(a.ControlId),
                    DefaultVal = a.DefaultVal,
                    Description = a.Description,
                    IsNotNull = a.IsNotNull,
                    Name = a.ColName,
                    Width = a.Width,
                    Sort = a.Sort
                });
            });
            return list.OrderBy(a => a.Sort).ToArray();
        }
    }
}
